package com.zecan.blog.vo.comment;

import com.zecan.blog.entity.Comment;
import lombok.Data;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * 返回前端的评论Vo
 * @ClassName CommentVo
 * @Description
 * @date 2022/11/11 11:55
 * @Version 1.0
 */
@Data
public class CommentVo {
    // 评论的id
    private Integer commentId;

    // 属于哪个文章的
    private Integer commentArticleId;

    // 发评论的用户id
    private Integer commentAuthorId;

    // 评论数量
    private Integer commentCount;

    // 当前回复的父级id  如果是父级则为-1
    private Integer commentParentId;

    // 评论或者回复的内容
    private String CommentContent;

    //评论创建时间
    private String commentCreateTime;

    // 当前评论的是哪个用户的用户名的
    private  String commentNickname;

    // 评论的用户头像
    private  String commentAuthorAvatar;

    // 回复的用户名
    private String commentReplyNickname;

    // 子级回复 没有为 null;
    private List<Comment> children = new ArrayList<>();


    public static CommentVo buildCommentVo(Comment comment, List<Comment> commentChildren) {
        CommentVo commentVo = new CommentVo();

        commentVo.commentArticleId = comment.getCommentArticleId();
        commentVo.CommentContent = comment.getCommentContent();
        commentVo.commentId = comment.getCommentId();
        // 根据所有子级回复进行数据筛选, 筛查属于自己的子级回复
        if(commentChildren != null){
            screeningComment(commentVo,commentChildren);
        }
        commentVo.commentParentId = comment.getCommentParentId();
        commentVo.commentAuthorAvatar = comment.getCommentAuthorAvatar();
        commentVo.commentCreateTime = comment.getCommentCreateTime();
        commentVo.commentNickname = comment.getCommentNickname();
        commentVo.commentAuthorId = comment.getCommentAuthorId();

        return commentVo;
    }

    public static CommentVo buildCommentVo(Comment comment) {
        CommentVo commentVo = new CommentVo();

        commentVo.commentArticleId = comment.getCommentArticleId();
        commentVo.CommentContent = comment.getCommentContent();
        commentVo.commentId = comment.getCommentId();
        if(comment.getCommentCount() != null) {
            commentVo.commentCount = comment.getCommentCount();
        }
        commentVo.commentParentId = comment.getCommentParentId();
        commentVo.commentAuthorAvatar = comment.getCommentAuthorAvatar();
        commentVo.commentCreateTime = comment.getCommentCreateTime();
        commentVo.commentNickname = comment.getCommentNickname();
        commentVo.commentReplyNickname = comment.getCommentReplyNickname();
        commentVo.commentAuthorId = comment.getCommentAuthorId();

        return commentVo;
    }

    /**
     * 筛查子级回复放入到父级中
     * @param commentVo  父级
     * @param commentChildren 子级回复集合
     */
    public static void screeningComment(CommentVo commentVo, List<Comment> commentChildren) {
        Iterator<Comment> iterator = commentChildren.iterator();

        Integer parentId = commentVo.getCommentId();
        // 循环筛入子元素 筛入成功删除当前子元素
        while(iterator.hasNext()) {
            Comment comment = iterator.next();
            Integer commentId = comment.getCommentParentId();
            if(commentId.equals(parentId)) {
                commentVo.children.add(comment);
                iterator.remove();
            }
        }
    }

}
